﻿using System;
using System.Collections.Generic;
using System.Linq;

using MSAcademic.v2.Infrastructure.AccessRights;
using MSAcademic.v2.Infrastructure.Article;
using MSAcademic.v2.Infrastructure.Login;
using MSAcademic.v2.Infrastructure.User;

namespace MSAcademic.v2.Web.Areas.Admin.Models.Article
{
    public partial class ArticleIndexModel
    {
        public int Page { get; set; }
    }

    public partial class ArticleIndexModel
    {
        public List<Tuple<IArticle,IUser>> Articles
        {
            get
            {
                if (_articles == null)
                {
                    _articles = new Lazy<List<Tuple<IArticle, IUser>>>(() =>
                        (from m in _articleQueries.GetLatestArticlesPaged(Page, true, this.SignedInUser).ToList()
                         let author= _userQueries.GetArticleAuthor(m.Id)
                         select new Tuple<IArticle,IUser>(m,author)).ToList());
}
                return _articles.Value;
            }
        }
        private Lazy<List<Tuple<IArticle, IUser>>> _articles;

        public int NumberOfPages
        {
            get
            {
                if (_numberOfPages == null)
                {
                    _numberOfPages = new Lazy<int>(() => _articleQueries.GetNumberOfPages(true));
                }
                return _numberOfPages.Value;
            }
        }
        private Lazy<int> _numberOfPages;
    }

    public partial class ArticleIndexModel
    {
        public IUser SignedInUser
        {
            get
            {
                if (_signedInUser == null)
                {
                    _signedInUser = new Lazy<IUser>(() => _loginService.TryGetSignedInUser());
                }
                return _signedInUser.Value;
            }
        }
        private Lazy<IUser> _signedInUser;

        public bool CanView
        {
            get
            {
                if (_canView == null)
                {
                    _canView = new Lazy<bool>(() => this.SignedInUser != null);
                }
                return _canView.Value;
            }
        }
        private Lazy<bool> _canView;

        public bool CanPublishAndHide
        {
            get
            {
                if (_canPublishAndHide == null)
                {
                    _canPublishAndHide = new Lazy<bool>(() => this.SignedInUser != null && _accessRightsService.CanUserAccess(this.SignedInUser, AccessRight.CanPublishAndHide));
                }
                return _canPublishAndHide.Value;
            }
        }
        private Lazy<bool> _canPublishAndHide;
    }

    public partial class ArticleIndexModel
    {
        private readonly IArticleQueries _articleQueries;
        private readonly ILoginService _loginService;
        private readonly IUserQueries _userQueries;
        private readonly IAccessRightsService _accessRightsService;

        public ArticleIndexModel(
            IArticleQueries articleQueries,
            ILoginService loginService,
            IUserQueries userQueries,
            IAccessRightsService accessRightsService)
        {
            _articleQueries = articleQueries;
            _loginService = loginService;
            _userQueries = userQueries;
            _accessRightsService = accessRightsService;
        }
    }
}